package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Constructionsection;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.domain.Projects;
import com.ruoyi.system.domain.ProjectsTwo;
import com.ruoyi.system.service.IProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 项目Controller
 *
 * @author ruoyi
 * @date 2020-12-16
 */
@RestController
@RequestMapping("/system/projects")
public class ProjectsController extends BaseController
{
    @Autowired
    private IProjectsService projectsService;


    /**
     * 查询项目列表
     */
    @PostMapping("/list")
    public TableDataInfo list(Projects projects)
    {
        //开启分页
       startPage();
       List<Projects> list = projectsService.selectProjectsList(projects);
        return getDataTable(list);
    }
    /**
     * 查询项目施工队
     */
    @PostMapping("/listct")
    public List<Constructionsection> listct()
    {
        List<Constructionsection> list = projectsService.selectconstructionsections();
        return list;
    }

    /**
     * 导出项目列表
     */
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(Projects projects)
    {
        List<Projects> list = projectsService.selectProjectsList(projects);
        ExcelUtil<Projects> util = new ExcelUtil<Projects>(Projects.class);
        return util.exportExcel(list, "projects");
    }


    /**
     * 新增保存项目
     */
    //@RequiresPermissions("system:projects:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody ProjectsTwo projects)
    {
        int a=projectsService.insertProjects(projects);
        //验证返回
        if(a==1) {
            return success();
        }else if(a==500) {
            return error("添加项目"+projects.getpName()+"失败,项目编号重复");
        }else {
            return error();
        }
    }
    /**
     * 修改项目
     */
    @GetMapping("/edit/{pId}")
    public AjaxResult edit(@PathVariable("pId") Long pId, ModelMap mmap)
    {
        Projects projects = projectsService.selectProjectsById(pId);
        mmap.put("projects", projects);
        return AjaxResult.success(projects);
    }
    /**
     * 修改保存项目
     */

    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody Project projects)
    {
        int i=projectsService.updateProject(projects);
        if (i>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 删除项目
     */
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping( "/remove/{pid}")
    public AjaxResult remove(@PathVariable long pid)
    {
        if(projectsService.deleteProjectsByIds(pid)>0) {
            return success();
        }else{
            return error("项目内有材料删除失败");
        }
    }

}
